/*
 * *新开医嘱公共方法（表格名字）
 * isChoseAll("xk_jy_table");
	isChoseAll("xk_jc_table");
	isChoseAll("west_medicine_table");
	isChoseAll("china_medicine_table");
	isChoseAll("xk_shoushu_table");
	isChoseAll("xk_zhiliao_table");
 * 
 */

//1 ichecke初始化
function iChenckInit(InputDom)
{
	InputDom.iCheck(
	{
	checkboxClass : 'icheckbox_square-blue',
	radioClass : 'iradio_square-blue',
	increaseArea : '20%' // optional
	});
}
// 全选事件
function isChoseAll(tableId)
{
	var all = $("#" + tableId + " thead").find("input[type='checkbox']");
	var sub = $("#" + tableId + " tbody").find("input[type='checkbox']");
	$(all).on('ifChecked', function(event)
	{
		$(sub).iCheck('check');
	});
	$(all).on('ifUnchecked', function(event)
	{
		$(sub).iCheck('uncheck');
	});
}

//紧急功能事件
function  jinjiChange(that){
	debugger;
	var isjinji = $(that).attr("data-value");
	if(isjinji == "1" ||isjinji == 1){
		$(that).text("");
		$(that).attr("data-value",0);
	}else{
		$(that).text("√");
		$(that).attr("data-value",1);
	}
}

// 页面表头chekbox增加
/* js增加表头的checbox,为了不使用icheck */
function theadAddCheckox(tableId)
{
	var html = "<input type='checkbox' value=''>";
	$("#" + tableId + " thead").find("td:eq(0)").empty().append(html);
	iChenckInit($("#" + tableId + " thead").find("input[type='checkbox']"));
}
//获取当前页签的表格ID和tbodyid
function activeTabParam(){
	var curtab = $("#order_tab").find(".active");
	var tabId = $(curtab).attr("id");
	var obj = {};
	obj.tabId = tabId;
	if(tabId.indexOf("Zhongyao") >=0){
		var isOdd = $("#"+tabId).find(".zhongyao_table tbody").find("tr");
		
		var firstT = $("#"+tabId).find(".zhongyao_table tbody").eq(0).find("tr");
		var secondT = $("#"+tabId).find(".zhongyao_table tbody").eq(1).find("tr");
		if(isOdd.length % 2 == 0){ //整除给第一个表格加行
			
			obj.tbodyclas = "china_medicine_tbody1"; //加行的表格id
			obj.tbodyclass_T = "china_medicine_tbody2"; //加数据的表格
			
		}else{//整除给第二个表格加行
			if(firstT.length  < secondT.length){
				
				obj.tbodyclas = "china_medicine_tbody1"; //加行的表格id
				obj.tbodyclass_T = "china_medicine_tbody1"; //加数据的表格
			}else{
				
				obj.tbodyclass_T = "china_medicine_tbody1"; //加数据的表格
				obj.tbodyclas = "china_medicine_tbody2";
			}
		}
		var chLastInput = $("#"+tabId).find("."+obj.tbodyclas).find(".zhongyao_yznr").last();
		if(chLastInput.length< 1 ){
			obj.inputId = -1;
		}else{
			obj.inputId =  $(chLastInput).parents("tr").find(".zhongyao_yznr").attr("id");
		}
		obj.chLastInput = chLastInput;
		
		
	}else{
		
		var tableId = $(curtab).find("table").attr("id");
		var tbodyId = $(curtab).find("table tbody").attr("id");
		if($("#"+tbodyId).find("tr").length>0){
			obj.inputId = $("#"+tbodyId).find("tr").last().find(".xiyao_yznr").attr("id");
		}else{
			obj.inputId = -1;
			
		}
		obj.tableId = tableId;
		obj.tbodyId = tbodyId;
	}
	return obj;
}


// 手动触发
function manualTrigger(activeAdviceInputId)
{
	var next = $("#" + activeAdviceInputId).parents("td").next().next();
	if(next.find("select").is(":visible")){
		fangxiang = "you";
		beforeAndAfter(next);
	}
}

// 重新分配组号
function assinGroupNo(table_id)
{
	var $trs = $('#' + table_id).find('tbody>tr');
	if ($trs.length)
	{
		$.each($trs, function(i, o)
		{
			$(o).find('td:eq(1)').text(i + 1);
		});
	}
}
//半角转全角
function ToCDB(str,$obj)   
{   
	var isCDB = false;
	var CDB =/^[\uFF10-\uFF19\uFF41-\uFF5A\uFF21-\uFF3A]*$/; //全角字符
	for(var k = 0; k <str.length;k++){
		if(CDB.test(str[k])){
			isCDB = true;
		}
	}
	if(isCDB){
	    var tmp = "";   
	    for(var i=0;i<str.length;i++)   
	    {   
	        if(str.charCodeAt(i)>65248&&str.charCodeAt(i)<65375){   
	            tmp += String.fromCharCode(str.charCodeAt(i)-65248);   
	        }else {   
	            tmp += String.fromCharCode(str.charCodeAt(i));   
	        }   
	    }   
	    $obj.val(tmp);
	    return tmp;
	}else{
		return str;
	}
	
}  

// td治疗方案点击增加遮罩
function tdCover(td)
{
	var width = $(td).parents("tr").width();
	var height = $(td).height();
	var position = $(td).position();
	$("<div id='tr_cover' name=\"div_cover\" style=\"width:" + width + "px;height:35px;left:0px;top:" + position.top + "px;opacity:1;position:absolute;z-index:1000;background-color:#e4e4e4;padding: 8px;\">加载中...</div>")
			.appendTo($(td).parents("tr"));
}

// 关闭div
function closeChangeTdDiv()
{
	$(".changeTdDiv").hide();
	$("#cover").addClass("hidden");
	$("#fenlei_html").slideUp(200); //存为方案关闭
	// $("#" + activeAdviceInputId).val("");
}

// 关闭自定义的搜索框
function closeSelfLayer()
{
	$("#tree_dorp_box_id").remove();
	$(".select-container").hide();
	// 关闭智搜框
	$(".smart_search").slideUp();
	$("#cover").addClass("hidden");
}

// //点击DIV之外的区域，div消失
//document.body.onclick = function(event)
//{
//	var e = event || window.event;
//	var elem = e.srcElement || e.target;
//	while (elem)
//	{
//		if (elem.className == "bootstrap-tagsinput" || elem.className == "selectDiv" || elem.className == "list-group-item node-drop_left_id" 
//			|| elem.className == "list-group-item node-tree" || elem.id == "tree-table" || elem.className == "laypage_main laypageskin_default" 
//				|| elem.className == "drop-container" ||$(elem).parents(".drop-container").length>0|| elem.className == "div_show" || elem.className == "list-group-item node-drop_left_id node-selected" 
//					 || $(elem).parents(".select-container").attr("id") == "neworder_zhenduan" 
//						|| $(elem).parents(".select-container").attr("id") == "neworder_div" || elem.className == "list-group-item node-tree node-selected"||
//				$(elem).hasClass("btn-drop")||elem.className=="elem.className"||$(elem).parents(".tag").length>0)
//		{
//			return;
//		}
//		elem = elem.parentNode;
//	}
//	closeSelfLayer();
////	if(activeAdviceInputId){
////		
////		adviceCheck();
////	}
//
//}
//医嘱内容校验
function adviceCheck(){
	var flag = $("#" + activeAdviceInputId).next().val();
	var flag2 = $("#" + activeAdviceInputId).val();
	var curT = $("#" + activeAdviceInputId).parents(".tab-pane").attr("id");
	var curM = $("#new_order_list").find("[href='#"+curT+"']").parents("li").find(".messageSign");
	if((!flag) && flag2 ){
		$("#" + activeAdviceInputId).select();
		layer.tips('请选择正确的医嘱', '#'+activeAdviceInputId);
	}else{
		if(!flag2){
			$("#" + activeAdviceInputId).next().val("");
		}
	}
	adviceCountShow();
}
//计算处方数量
function adviceCountShow(table_id){
	var cur_table_id;
	var cur_tab;
	var curtable;
	if(table_id){
		cur_table_id = table_id;
		cur_tab = $("#new_order_list").find("a[href='#Zhiliao']").parents("li").find(".messageSign");
	}else{
		
		var cur_top = $("#new_order_list").find("li.active a").attr("href");
		if(cur_top.indexOf("#Zhongyao") >=0){
			curtable = $(cur_top).find(".zhongyao_table");
			
		}else{
			
			cur_table_id =$(cur_top).find("table").attr("id");
			curtable = $("#"+cur_table_id);
		}
		cur_tab = $("#new_order_list").find("li.active").find(".messageSign");
	}
	var countAdvice=0;
	var isIllage = false;
	$(curtable).find("tbody  tr").each(function(i,e){
		if($(e).find("textarea").val()){
			countAdvice++;
			 if(!$(e).find("textarea").next().val()){
				 isIllage  = true;
			 }
		}
	});
	if(countAdvice>0){
		if(isIllage){
			cur_tab.css("background-color","red");
		}else{
			
			cur_tab.css("background-color","#259427");
		}
		cur_tab.css("display","block");
		cur_tab.find(".label-normal").text(countAdvice);
		countAdvice=0;
	}else{
		cur_tab.css("display","none");
	}
}
//关闭layer
function  cancelAdvice(){
	execIframe();
	closeAndRefresh_parent();
	closeAndRefresh();
	parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭弹出层
	}


//跨域保存
function execIframe(){
	debugger;
	var now =  new Date($.ajax({async: false}).getResponseHeader("Date"));
	var dd = now.Format1('yyyy-MM-dd hh:mm:ss:S',1);//时间到时分秒毫秒
	if($("#shuxiebingli").attr("src")){
		debugger;
		var layId = "emr"
			if(typeof(exec_obj)=='undefined'){
				exec_obj = document.createElement('iframe');
				exec_obj.name = 'tmp_frame';
				exec_obj.src = dzbl+'/out_interface_doctor1.jsp?layId='+ layId+'&lastDate='+dd;
				exec_obj.style.display = 'none';
				document.body.appendChild(exec_obj);
			}else{
				exec_obj.src = dzbl+'/out_interface_doctor1.jsp?layId='+ layId+'&lastDate='+dd + '&date=' + Math.random();
			}
	}
}

//新开中删除医嘱单方法，（公用，出来西药的有同组）

function delAdviceTr(){
	var params=activeTabParam();
	var arr = $("#"+params.tbodyId+" td:nth-of-type(1) input:checked");
	var trArr = $("#"+params.tbodyId+" tr");
	if (arr.length)
	{
		layer.confirm('确定要删除所选项',
		{
			btn : [
			'确定', '取消'
			],
			icon:3,
		// 按钮
		}, function()
		{
			$("#"+params.tbodyId+" tbody td:nth-of-type(1) input:checked").each(function()
			{ // 遍历选中的checkbox
				$(this).parents("tr").remove();
			});
			assinGroupNo(params.tableId);
			layer.msg('删除成功',
			{
			time : 2000,
			icon : 1,
			skin: 'layui-layer-msg1'
			});
			if (arr.length == trArr.length)
			{
				eval(GLOBALSUN[topTab]["inittable"]+"()");
			}
		}, function()
		{
		});
	}
	else
	{
		layer.msg('请选择要删除的处方',
		{
		time : 2000,
		icon : 0,
		skin: 'layui-layer-msg0'
		});
	}
}
//自定义诊断
/*function selfDiagnoseEvent(tpltype){
	var url = rootUrl+ "/clidoctor/queryBce1_vao1";
	var text = $("#search_jibing").val();
	var para = {
					bce01:bce01,
					text:"%"+text+"%"
				}
	var param = {
			      params:JSON.stringify(para)
				 };
	$.when($.get(url, param)).done(function(data) {
		var list = data.list;
		var obj = {};
		obj.list = list;
		obj.type = tpltype;
		laytplUrl_T(myurl + "/module/self-diagnose.html", obj, function(data, html)
			{
				$("#self-diagnose").empty().append(html);
				debugger;
				if(tpltype == "manage"){
					for(var i=0;i< list.length;i++){
						var str1 = '{"bak01":"'+list[i].bak01+'","bak02":"'+list[i].bak02+'"}';
						$("#self-diagnose").find(".add_self_d:eq("+i+")").tagsinput('add', list[i].bak05,str1);
					}
					selfDiagnoseDrop(); //加载诊断搜索框;
				}
			})		
	})
}*/
/*//自定义诊断管理
function manageChangeEvent(){
	selfDiagnoseEvent("manage");
}*/
/*
//自定义诊断保存
function saveSelfDiagnose(){
	debugger;
	var vaoList = [];
	$("#self-diagnose-table tbody tr").each(function(i,e){
		var obj={};
		obj.bce01= bce01;
		var diagnose=  $(e).find(".label-info");
		var diaostr = diagnose.attr("data-info");
		var diaobj = JSON.parse(diaostr);
		if(diaobj == -999){
			obj.bak01 =  diaobj; //id
			obj.bak02 =  $(e).find(".self-code").val(); //编码
			
		}else if(diaobj.bak01 == -999){
			obj.bak01 =  diaobj.bak01; //id
			obj.bak02 =  $(e).find(".self-code").val(); //编码
		}
		else{
			
			obj.bak01 =  diaobj.bak01; //id
			obj.bak02 =  diaobj.bak02; //id
		}
		obj.bak05 =  diagnose.text().trim(); //名字
//		obj.abbrp= makePy(obj.bak05);   //拼音
		obj.rownr= $(e).find(".self-num").val();
		if(obj.bak05){
			vaoList.push(obj);
		}
	})
	var url = rootUrl+ "/clidoctor/updateBce01_vao1";
	var para = {
				bce01:bce01,
				vaoList:vaoList
				}
	var param = {
			      params:JSON.stringify(para)
				 };
	$.when($.post(url, param)).done(function(data) {
		debugger;
		selfDiagnoseEvent("show");
		
	})
}*/

//自定义诊断新增
/*function addSelfDiagnose(){
//获取最大行号
	var maxnum=0;
	$("#self-diagnose-table tbody tr").each(function(i,e){
		var num = $(e).find(".self-num").val();
		if(parseInt(maxnum) < parseInt(num)){
			maxnum = num;
		}
	})	
	var data = {};
	data.maxnum = parseInt(maxnum)+1;
	laytplUrl_T(myurl + "/module/self-diagnose-add.html", data, function(data, html)
		{
			$("#self-diagnose-table").prepend(html);
			disableForFirst();
			selfDiagnoseDrop(); //加载诊断搜索框
		})	
}*/
//自定义诊断取消
/*function cancelSelfDiagnose(){
	selfDiagnoseEvent("show");
}
*/
/*function upmove(that){
	var $cur_row = $(that).parents("tr");
	$cur_row.fadeOut(100,function(){
	    var $prev_row = $cur_row.prev();
	    $prev_row.before($cur_row);
	    $cur_row.fadeIn(100, function(){
	    	disableForFirst();
	    });
	});
	exchangeRownr(that,"before");
}
function downmove(that){
	var $cur_row = $(that).parents("tr");
	$cur_row.fadeOut(100,function(){
	    var $next_row = $cur_row.next();
	    $next_row.after($cur_row);
	    $cur_row.fadeIn(100, function(){
	    	disableForFirst();
	    });
	});
	exchangeRownr(that,"next");
	
}
function exchangeRownr(that,o){
	var $cur_row = $(that).parents("tr");
	var cur_tr = $cur_row.find(".self-num"); //当前诊断的rownr
	var cur_rownr = cur_tr.val();
	var other_tr;
	if(0 == "next"){
		 other_tr = $cur_row.next().find(".self-num");
	}else{
		
		 other_tr = $cur_row.prev().find(".self-num");
	}
	var other_rownr = other_tr.val();
	var temp = cur_rownr;
	cur_rownr.val(other_rownr);
	other_rownr.val(temp);
}
function deletmove(that){
	event.stopPropagation(); 
	$(that).parents("tr").remove();
	disableForFirst();
}

function disableForFirst(){
	$("#self-diagnose-table").find("button").removeAttr("disabled")
	$("#self-diagnose-table").find(".glyphicon-circle-arrow-up").first().parents("button").attr("disabled",true);
	$("#self-diagnose-table").find(".glyphicon-circle-arrow-down").last().parents("button").attr("disabled",true);
}
var activeself ;
function selfDiagnoseDrop(){
	$(".self-diagnose-drop").off("click").on("click",function(){
		 var e=arguments.callee.caller.arguments[0]||event; 
		 if (e && e.stopPropagation) { 
		  e.stopPropagation(); 
		 } else if (window.event) { 
		  window.event.cancelBubble = true; 
		 } 
		  activeself = $(this);
		 if($("#self-diagnose-drop").is(":visible")){
			 $("#self-diagnose-drop").slideUp();
		 }else{
			 
			 var thisLeft=$(this).parents(".input-group").position().left;
			 var thisTop=$(this).parents(".input-group").position().top;
			 var thisheight=$(this).outerHeight()+1;
			 $("#self-diagnose-drop").slideDown();
			 $("#self-diagnose-drop").css({
				 position:"absolute",
				 left:thisLeft,
				 top:thisTop+thisheight+"px",
				 display:"block"
			 })
			 $("#self-diagnose-drop").css({
				 "z-index":999 
			 })
			 var param = {};
			 param.inputid = "self-diagnose-input";
			 param.btnid = "self-diagnose-btn";
			 param.tableid = "self-diagnose-drop-table";
			 diagnoseAllEvent(param);
		 }
	})
}
*/






